/*
 *  This class is part of WorkTimeCalculator Android App
 *  Class derived from code of project wheel-demo (http://code.google.com/p/android-wheel/)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.dpndeveloper.wtc.util.widget;

import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import android.content.Context;

import com.dpndeveloper.wtc.adapter.DateArrayAdapter;
import com.dpndeveloper.wtc.adapter.DateNumericAdapter;
import com.dpndeveloper.wtc.listener.OnDateChangedListener;

/**
 * This class allow to use three wheels as a widget to manage a dsate
 * 
 * @author Domenico Pio Novelli
 * 
 */
public class DateWidget implements OnWheelChangedListener {

	private static final int INITIAL_YEAR = 2012;
	/* The WheelView for the day */
	private WheelView							day;
	/* The WheelView for the month */
	private WheelView							month;
	/* The WheelView for the year */
	private WheelView							year;
	/* the associated context */
	private Context								context;
	/* list of listener to notify changes in the wheels */
	private final List<OnDateChangedListener>	listeners;

	/**
	 * Constructor
	 * 
	 * @param context
	 *            the related context
	 * @param day
	 *            The WheelView for the day
	 * @param month
	 *            The WheelView for the month
	 * @param year
	 *            The WheelView for the year
	 */
	public DateWidget(Context context, WheelView day, WheelView month,
			WheelView year) {

		this.day = day;
		this.month = month;
		this.year = year;

		this.context = context;
		this.listeners = new ArrayList<OnDateChangedListener>();

		Calendar calendar = Calendar.getInstance(TimeZone.getDefault());

		// month
		int curMonth = calendar.get(Calendar.MONTH);
		String months[] = new String[12];

		// DateFormatSymbols().getMonths() returns 13 elements!
		String symbolsMonths[] = new DateFormatSymbols().getMonths();
		for (int i = 0; i < 12; i++) {
			months[i] = symbolsMonths[i];
		}

		month.setViewAdapter(new DateArrayAdapter(context, months, curMonth));
		month.setCurrentItem(curMonth);
		month.addChangingListener(this);

		// year
		int curYear = calendar.get(Calendar.YEAR);
		year.setViewAdapter(new DateNumericAdapter(context, INITIAL_YEAR, curYear, curYear-INITIAL_YEAR));
		year.setCurrentItem(curYear-INITIAL_YEAR);
		year.addChangingListener(this);

		updateDays();

		if (day != null) {
			// day
			day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH) - 1);
			day.addChangingListener(this);
		}
	}

	/**
	 * Adds the given OnDateChanged listener
	 * 
	 * @param listener
	 *            the listener to add
	 */
	public void addOnDateChangedListener(OnDateChangedListener listener) {
		listeners.add(listener);
	}

	/**
	 * Removes the given OnDateChanged listener
	 * 
	 * @param listener
	 *            the listener to remove
	 */
	public void removeOnDateChangedListener(OnDateChangedListener listener) {
		listeners.remove(listener);
	}

	/**
	 * Returns the current selected date
	 * 
	 * @return the current selected date
	 */
	public Calendar getSelectedDate() {
		Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
		calendar.set(Calendar.YEAR,
				INITIAL_YEAR + year.getCurrentItem());
		calendar.set(Calendar.MONTH, month.getCurrentItem());
		calendar.set(Calendar.DAY_OF_MONTH,
				day != null ? day.getCurrentItem() + 1 : 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar;
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		if (wheel.equals(year) || wheel.equals(month)) {
			updateDays();
		}
		notifyChange();
	}

	/**
	 * notify changes to registered listeners
	 */
	private void notifyChange() {
		for (OnDateChangedListener listener : listeners) {
			listener.onDateChanged(this);
		}
	}

	/**
	 * Updates day wheel. Sets max days according to selected month and year
	 */
	private void updateDays() {
		if (day != null) {
			Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
			calendar.set(Calendar.YEAR,
					INITIAL_YEAR + year.getCurrentItem());
			calendar.set(Calendar.MONTH, month.getCurrentItem());

			int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
			day.setViewAdapter(new DateNumericAdapter(context, 1, maxDays,
					calendar.get(Calendar.DAY_OF_MONTH) - 1));
			int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
			day.setCurrentItem(curDay - 1, true);
		}
	}

}
